package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Models.UserProfile;

public class MarkCouponsHandler  {
	
	static final String STATUS = "status";
	static final String RESPONSE = "response";
	static final  String ERROR = "error";

	
	String feedUrl;
	 boolean discussBool;
	 static final  String MESSAGE = "message";
		
		
	Context context;
    private static final String SOAP_ACTION = "1";
    private static final String METHOD_NAME = "bigfreakindeals";
    private static final String NAMESPACE = "1";
    
    
	public MarkCouponsHandler(String feedUrl) {
		this.feedUrl = feedUrl;
		parse();
		System.out.println("URL: "+feedUrl);
	}
	
	 public void parse() {

	     try {
		 
	    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.setOutputSoapObject(request);
		
		    HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);

			try{
				androidHttpTransport.call(SOAP_ACTION, envelope);
			}catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
			SoapObject response = (SoapObject) envelope.getResponse();

			 String discuss  = response.getProperty(STATUS).toString();
	       		 if (discuss !=null && discuss.length() >0){
	       			 if (discuss.contentEquals("false")){
	       				discussBool = false;
	       			 }else{
	       				discussBool = true;
	       			 }
	       		 }
						
	    		if(discussBool){
					Intent intent = new Intent(Constant.EVENTS_MARKUSED_COUPON_SUCCESS);
					ApplicationEx.getInstance().sendBroadcast(intent);
				}else{
					String error = response.getProperty(ERROR).toString();
					Intent intent = new Intent(Constant.EVENTS_MARKUSED_COUPON_ERROR);
					intent.putExtra(Constant.TAG_WEBSERVICE_ERROR_MESSAGE,error);
					ApplicationEx.getInstance().sendBroadcast(intent);
					return;
					
				}
			
	     } catch (IOException e1) {
				// TODO Auto-generated catch block
	    		
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
		
			} catch (Exception e1) {
				
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
	     }
}